home *** CD-ROM | disk | FTP | other *** search
/ WINMX Assorted Textfiles / Ebooks.tar / Text - Tech - Hacking - Web Proxy Hacking (TXT).rar / webproxy.txt
Text File  |  2003-09-28  |  6KB  |  141 lines

  1. #! /bin/sh
  2. ## Web proxy, following the grand tradition of Web things being handled by
  3. ## gross scripts.  Uses netcat to listen on a high port [default 8000],
  4. ## picks apart requests and sends them on to the right place.  Point this
  5. ## at the browser client machine you'll be coming from [to limit access to
  6. ## only it], and point the browser's concept of an HTTP proxy to the
  7. ## machine running this.  Takes a single argument of the client that will
  8. ## be using it, and rejects connections from elsewhere.  LOGS the queries
  9. ## to a configurable logfile, which can be an interesting read later on!
  10. ## If the argument is "reset", the listener and logfile are cleaned up.
  11. ##
  12. ## This works surprisingly fast and well, for a shell script, although may
  13. ## randomly fail when hammered by a browser that tries to open several
  14. ## connections at once.  Drop the "maximum connections" in your browser if
  15. ## this is a problem.
  16. ##
  17. ## A more degenerate case of this, or preferably a small C program that
  18. ## does the same thing under inetd, could handle a small site's worth of
  19. ## proxy queries.  Given the way browsers are evolving, proxies like this
  20. ## can play an important role in protecting your own privacy.
  21. ##
  22. ## If you grabbed this in ASCII mode, search down for "eew" and make sure
  23. ## the embedded-CR check is intact, or requests might hang.
  24. ##
  25. ## Doesn't handle POST forms.  Who cares, if you're just watching HTTV?
  26. ## Dumbness here has a highly desirable side effect: it only sends the first
  27. ## GET line, since that's all you really ever need to send, and suppresses
  28. ## the other somewhat revealing trash that most browsers insist on sending.
  29. ##
  30. ## *Hobbit*, 951021 with many subsequent improvements
  31.  
  32. # set these as you wish: proxy port...
  33. PORT=8000
  34. # logfile spec: a real file or /dev/null if you don't care
  35. LFILE=${0}.log
  36. # optional: where to dump connect info, so you can see if anything went wrong
  37. # CFILE=${0}.conn
  38. # optional extra args to the listener "nc", for instance "-s inside-net-addr"
  39. # XNC=''
  40.  
  41. # functionality switch has to be done fast, so the next listener can start
  42. # prelaunch check: if no current client and no args, bail.
  43. case "${1}${CLIENT}" in
  44.   "")
  45.     echo needs client hostname
  46.     exit 1
  47.   ;;
  48. esac
  49.  
  50. case "${1}" in
  51.   "")
  52. # Make like inetd, and run the next relayer process NOW.  All the redirection
  53. # is necessary so this shell has NO remaining channel open to the net.
  54. # This will hang around for 10 minutes, and exit if no new connections arrive.
  55. # Using -n for speed, avoiding any DNS/port lookups.
  56.     nc -w 600 -n -l -p $PORT -e "$0" $XNC "$CLIENT" < /dev/null > /dev/null \
  57.     2> $CFILE &
  58.   ;;
  59. esac
  60.  
  61. # no client yet and had an arg, this checking can be much slower now
  62. umask 077
  63.  
  64. if test "$1" ; then
  65. # if magic arg, just clean up and then hit our own port to cause server exit
  66.   if test "$1" = "reset" ; then
  67.     rm -f $LFILE
  68.     test -f "$CFILE" && rm -f $CFILE
  69.     nc -w 1 -n 127.0.0.1 $PORT < /dev/null > /dev/null 2>&1
  70.     exit 0
  71.   fi
  72. # find our ass with both hands
  73.   test ! -f "$0" && echo "Oops, cannot find my own corporeal being" && exit 1
  74. # correct launch: set up client access control, passed along thru environment.
  75.   CLIENT="$1"
  76.   export CLIENT
  77.   test "$CFILE" || CFILE=/dev/null
  78.   export CFILE
  79.   touch "$CFILE"
  80. # tell us what happened during the last run, if possible
  81.   if test -f "$CFILE"  ; then
  82.     echo "Last connection results:"
  83.     cat $CFILE
  84.   fi
  85.  
  86. # ping client machine and get its bare IP address
  87.   CLIENT=`nc -z -v -w 8 "$1" 22000 2>&1 | sed 's/.*\[\(..*\)\].*/\1/'`
  88.   test ! "$CLIENT" && echo "Can't find address of $1" && exit 1
  89.  
  90. # if this was an initial launch, be informative about it
  91.   echo "=== Launch: $CLIENT" >> $LFILE
  92.   echo "Proxy running -- will accept connections on $PORT from $CLIENT"
  93.   echo "  Logging queries to $LFILE"
  94.   test -f "$CFILE" && echo "  and connection fuckups to $CFILE"
  95.  
  96. # and run the first listener, showing us output just for the first hit
  97.   nc -v -w 600 -n -l -p $PORT -e "$0" $XNC "$CLIENT" &
  98.   exit 0
  99. fi
  100.  
  101. # Fall here to handle a page.
  102. # GET type://host.name:80/file/path HTTP/1.0
  103. # Additional: trash
  104. # More: trash
  105. # <newline>
  106.  
  107. read x1 x2 x3 x4
  108. echo "=== query: $x1 $x2 $x3 $x4" >> $LFILE
  109. test "$x4" && echo "extra junk after request: $x4" && exit 0
  110. # nuke questionable characters and split up the request
  111. hurl=`echo "$x2" | sed -e "s+.*//++" -e 's+[\`'\''|$;<>{}\\!*()"]++g'`
  112. # echo massaged hurl: $hurl >> $LFILE
  113. hh=`echo "$hurl" | sed -e "s+/.*++" -e "s+:.*++"`
  114. hp=`echo "$hurl" | sed -e "s+.*:++" -e "s+/.*++"`
  115. test "$hp" = "$hh" && hp=80
  116. hf=`echo "$hurl" | sed -e "s+[^/]*++"`
  117. # echo total split: $hh : $hp : $hf >> $LFILE
  118. # suck in and log the entire request, because we're curious
  119. # Fails on multipart stuff like forms; oh well...
  120. if test "$x3" ; then
  121.   while read xx ; do
  122.     echo "${xx}" >> $LFILE
  123.     test "${xx}" || break
  124. # eew, buried returns, gross but necessary for DOS stupidity:
  125.     test "${xx}" = "" && break
  126.   done
  127. fi
  128. # check for non-GET *after* we log the query...
  129. test "$x1" != "GET" && echo "sorry, this proxy only does GETs" && exit 0
  130. # no, you can *not* phone home, you miserable piece of shit
  131. test "`echo $hh | fgrep -i netscap`" && \
  132.   echo "access to Netscam's servers <b>DENIED.</b>" && exit 0
  133. # Do it.  30 sec net-wait time oughta be *plenty*...
  134. # Some braindead servers have forgotten how to handle the simple-query syntax.
  135. # If necessary, replace below with (echo "$x1 $hf" ; echo '') | nc...
  136. echo "$x1 $hf" | nc -w 30 "$hh" "$hp" 2> /dev/null || \
  137.   echo "oops, can't get to $hh : $hp".
  138. echo "sent \"$x1 $hf\" to $hh : $hp" >> $LFILE
  139. exit 0
  140.  
  141.